package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.NotSelectException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    SetmealService setmealService;
    @Autowired
    SetmealDishService setmealDishService;

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result insert(@RequestBody SetmealDTO setmealDTO){
        setmealService.insert(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> setmealPageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setmealService.setmealPageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    

    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐起售状态")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result modifyStatus(@PathVariable int status , long id ){
        setmealService.modifyStatus(status, id);
        return Result.success();
    }

    @ApiOperation("删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetmeal(long[] ids){
        //起售中的套餐不能删除
        if(ids.length!=0 && ids!=null){
        int[] statuss = setmealService.findStatus(ids);
        for (int status : statuss) {
            if(status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealService.deleteSetmeal(ids);
        }else {
            throw new NotSelectException("未选中套餐");
        }
        return Result.success();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> selectSetmealById(@PathVariable long id){
        SetmealVO setmealVO = setmealService.selectSetmealById(id);
        return Result.success(setmealVO);
    }


    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("要修改的套餐：{}",setmealDTO);
        //要先删除该套餐下的所有菜品，以免重复
        setmealDishService.deleteConnection(setmealDTO.getId());
        //由于新增的菜品可能是停售的，修改后统一将套餐也修改为停售的
        setmealDTO.setStatus(StatusConstant.DISABLE);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

}
